package multiThread.assignment;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;

public class StringSearch extends Thread {
	String s1, s2, s3, s4;
	int ttt;

	public StringSearch(String s1, String s2, String s3, String s4) {
		this.s1 = s1;
		this.s2 = s2;
		this.s3 = s3;
		this.s4 = s4;
	}

	public synchronized void run() {
		String str = "", temp = "";
		boolean val = false;
		Thread t = Thread.currentThread();
		String name = t.getName();
		System.out.println("Thread: " + name);
		File f1 = new File("sqlBuffer.txt");
		try {

			FileReader fr = new FileReader(f1);
			BufferedReader br = new BufferedReader(fr);
			if (name.equals("First Thread"))
				str = s1;
			else if (name.equals("Second Thread"))
				str = s2;
			else if (name.equals("Third Thread"))
				str = s3;
			else if (name.equals("Fourth Thread")) str = s4;
			String ch;
			System.out.print("str " + str);
			System.out.println();
			// char ccc[]=str.toCharArray();
			// int tt=0;
			while ((ch = br.readLine()) != null) {

				StringTokenizer st = new StringTokenizer(ch, " ");
				while (st.hasMoreTokens()) {
					String ss = st.nextToken();
					if (ss.equals(str)) {
						ttt++;
					}
				}

			}
			if (ttt > 0) {
				System.out.println("Found in file");
			} else {
				System.out.println("Not Found in file");
			}
			ttt = 0;

			fr.close();

		} catch (EOFException e) {
			System.out.println(str + " NOT found in file");
		} catch (Exception e) {

		}

	}
}